<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of SitioController
 *
 * @author Nayeli
 */
class RouteController extends AppController
{
    public function beforeFilter()
    {
        parent::beforeFilter();
    }

    public function index()
    {
       $this->set('title_for_layout', 'Rutas Disponibles'); 
       $this->layout = 'base';
      
       $limit=10;
       $this->Route->routesList($limit);
      
       
       $this->set('routesDetails', $this->Route->routesList($limit));
       
       $imagen = 'banner/masfletes_2.jpg';
       $altima = 'Masfletes_Servicios';
       $this->set('imagen',$imagen);
       $this->set('altima',$altima);
    }
    
    public function publicaRutaExpress() {
       $this->set('title_for_layout', 'Publica tu ruta gratis'); 
       $this->layout = 'base';
       
       
       $this->loadModel("State");
       $this->set('estados', $this->parseForSend($this->State->find('all', array(
           'fields' => array('id', 'name')
           ))));
       $this->loadModel("Vehicle");
       $this->set('vehiculos', $this->parseForSend($this->Vehicle->find('all', array(
           'fields' => array('id', 'name')
           ))));
       $this->loadModel("VehicleType");
       $this->set('tVehiculos', $this->parseForSend($this->VehicleType->find('all', array(
           'fields' => array('id', 'name')
           ))));
       
       if($this->request->is("post"))
       {
            $datos = $this->request->data;
            $usuario = array();
            $usuario['username'] = $datos['correo_registrarse'];
            $usuario['type'] = 3;
            $this->loadModel("User");
            try
            {
                $var = $this->User->save($usuario);
                $route = array();
                $route['user_id'] = $var['User']['id'];
                $route['vehicle_id'] = $datos['vehiculo'];
                $route['load_availability_date'] = $datos['disponibilidad'];
                $route['comments'] = $datos['comentarios'];
                $route['effective_days'] = $datos['vigencia'];
                $route['vehicleType_id'] = $datos['tipoVehiculo'];
                $route['stateOrigin_id'] = $datos['eOrigen'];
                $route['stateDestiny_id'] = $datos['eDestino'];
                $route['municipalityOrigin_id'] = $datos['mOrigen'];
                $route['municipalityDestiny_id'] = $datos['mDestino'];
                try
                {
                    $this->loadModel('Route');
                    $this->Route->save($route);
                    $this->set('rerror', false);
                }
                catch (PDOException $e)
                {
                    $this->set('rerror', true);
                }
            }
            catch (PDOException $e)
            {
                $this->set('rerror', true);
            }
       }
       $this->set('imagen',$imagen);
       $this->set('altima',$altima);
    }
    
    public function dameMunicipios()
    {
        $this->loadModel("Citie");
        $municipios = $this->parseForSend($this->Citie->find('all', array(
            'conditions' => array("state_id" => $this->request->data['id']),
            'fields' => array('id', 'name')
        )));
        $this->set(array('municipios'=>$municipios, '_serialize'=>'municipios'));
        $this->viewClass = 'Json';
    }

    private function parseForSend($array)
    {
        $nArray = array();
        
        foreach ($array as $value) {
            $var = each($value);
            $nArray[$var[1]['id']] = $var[1]['name'];
        }
        
        return $nArray;
    }
}

?>
